<?php


namespace App\backend\services;


use App\backend\models\AdminRoleMenuModel;
use App\backend\models\AdminUsersModel;
use App\backend\models\CategoryModel;
use App\backend\models\MenuModel;
use App\Exceptions\InvalidRequestException;

class MenuServices
{

    /**
     * 获取 - 根据菜单权限关系，菜单路由结构树列表
     *
     * @param $data //角色ID 数组
     * @return array
     */
    public static function getMenuRoleList($data)
    {
        if (!isset($data) || empty($data)) {
            throw new InvalidRequestException(10009);
        }

        $list = AdminRoleMenuModel::getMenuRoleList($data['id']);
        return CategoryModel::getTree($list);
    }

    /**
     * 获取 - 根据菜单权限关系，菜单路由列表
     *
     * @param $data //角色ID 数组
     * @return array
     */
    public static function getRoleMenuUriList($data)
    {
        if (!isset($data) || empty($data)) {
            throw new InvalidRequestException(10009);
        }

        $list = AdminRoleMenuModel::getRoleMenuUriList($data['id']);
        if (!empty($list)) {
            $list = array_column($list, 'uri');
        }

        return $list;
    }

    /**
     * 获取 - 菜单列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {
        $where_data = [];
        if (isset($data['name']) && !empty($data['name'])) {
            $where_data[] = ['name','like',"%{$data['name']}%"];
        }

        $select_data = ['id', 'parent_id', 'title', 'icon', 'uri', 'permission'];
        $list = MenuModel::listed($where_data, $select_data);
        return CategoryModel::getTree($list);
    }

    /**
     * 获取 - 角色详情
     *
     * @param $data
     * @return array
     */
    public static function getDetail($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = !empty($data['menu_id']) ? $data['menu_id'] : 0;unset($data['menu_id']);

        $list = MenuModel::info($data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }
        return $list;
    }

    /**
     * 添加/编辑 - 角色
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function modify($data)
    {
        if (empty($data) && $data == false) {
            throw new InvalidRequestException(10009);
        }

        $id = isset($data['menu_id']) && !empty($data['menu_id']) ? $data['menu_id'] : 0;unset($data['menu_id']);

        $where_data = [];
        if ($id) {
            $data['updated_at'] = time();
            $where_data[] = ['id', '=', $id];
            MenuModel::updatedData($where_data, $data);
        } else {
            $data['created_at'] = time();
            $insert_id = MenuModel::insertGetIdData($data);
            if ($insert_id) {
                $where_data[] = ['id', '=', $insert_id];
            }
        }

        if (empty($where_data)) {
            throw new InvalidRequestException(20013);
        }

        $select_data = ['id', 'parent_id', 'title', 'icon', 'uri', 'permission'];
        $list = MenuModel::info($where_data, $select_data);
        if (empty($list)) {
            throw new InvalidRequestException(20013);
        }

        return $list;
    }

    /**
     * 删除 - 操作员
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function delete($data)
    {
        if (empty($data) && $data == false) {
            throw new InvalidRequestException(10009);
        }

        $data['id'] = !empty($data['menu_id']) ? $data['menu_id'] : 0;unset($data['menu_id']);
        $update_data['status'] = 1;
        $update_data['updated_at'] = time();

        $list = AdminUsersModel::deleteds($data, $update_data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }
        return [];
    }

}
